<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;

class BaseController extends Controller{
	//初始化
	protected function _initialize(){
    	$this->checkLogin();
    	$this->checkPermit2(CONTROLLER_NAME,ACTION_NAME);
    	$this->publicAssign();
    	
    	//print_r(get_defined_constants());
    }
    
    //注入公共变量
    protected function publicAssign(){
    	$this->assign('Admin',session(C('SESSION_KEY')));
    	$this->assign('keyword',I('get.keyword'));
    }
    
    //数据分页
    protected function page($total){
    	$Page = new Page($total,C('PAGE_SIZE'));
    	$Page->setConfig('first','首页');
    	$Page->setConfig('prev','上一页');
    	$Page->setConfig('next','下一页');
    	$Page->setConfig('last','尾页');
    	$Page->setConfig('theme','<form method="get" action="" class="page"><p>%NOW_PAGE%/%TOTAL_PAGE%页（每页'.$Page->listRows.'条/共%TOTAL_ROW%条） | %FIRST% %UP_PAGE% %DOWN_PAGE% %END% %LINK_PAGE%</p></form>');
    	$this->assign('page',$Page->show());
    	return $Page->firstRow;
    }
    
    //成功提示
    protected function success($url='',$tip='',$second=3,$type=0){
    	$url = htmlspecialchars_decode($url);
    	if (empty($tip)){
    		header('Location:'.$url);
			exit;
    	}else{
			if (in_array($type,array(0,2))){
				$this->assign('Refresh',$second);
				$this->assign('Url',$url);
				$this->assign('Kind','success');
				$this->assign('Tip',$tip);
				$this->assign('Type',$type);
				$this->assign('A','如果您的浏览器没有自动跳转，请点击这里');
				$this->assign('Run',"<script type=\"text/javascript\">var run=window.parent.document.getElementById('run');if(run!=null)run.innerHTML='执行耗时：'+".(_runtime()-START_TIME)."+'秒';</script>");
				$this->display(C('TMPL_ACTION_SUCCESS'));
				exit;
			}elseif ($type == 1){
				exit("<script type=\"text/javascript\">alert('$tip');parent.location.href='$url';</script>");
			}else{
				$this->success($url,$tip,$second,0);
			}
    	}
    }
    
    //错误提示
    protected function failed($tip='',$second=5,$type=0,$url=''){
    	$url = htmlspecialchars_decode($url);
		$this->assign('Refresh',$second);
		$this->assign('Url',$url);
		$this->assign('Kind','failed');
		$this->assign('Tip',$tip);
		$this->assign('Type',$type);
		if ($type == 0){
			$this->assign('A','点击这里返回上一页');
		}elseif ($type == 1){
			$this->assign('A','如果您的浏览器没有自动跳转，请点击这里');
		}
		$this->assign('Run',"<script type=\"text/javascript\">var run=window.parent.document.getElementById('run');if(run!=null)run.innerHTML='执行耗时：'+".(_runtime()-START_TIME)."+'秒';</script>");
		$this->display(C('TMPL_ACTION_ERROR'));
		exit;
    }
    
    //确认提示框
	protected function confirm($message){
		$this->failed('<form method="post" action=""><input type="hidden" name="prev" value="'.C('PREV_URL').'"><p>'.$message.'</p><p><input type="submit" value="确定" class="submit"> <input type="button" value="取消" onclick="history.back();" class="submit"></p></form>',0,2);
	}
	
	//登录验证
	protected function checkLogin(){
		if (session('?'.C('SESSION_KEY')) && CONTROLLER_NAME=='Login' && ACTION_NAME=='index'){
			$this->success(U('Index/index'));
		}elseif (!session('?'.C('SESSION_KEY')) && CONTROLLER_NAME=='Index'){
			$this->success(U('Login/index'));
    	}elseif (!session('?'.C('SESSION_KEY')) && CONTROLLER_NAME!='Login'){
    		$this->failed('非法登录！',5,1,U('Login/index'));
    	}
	}
	
	//权限验证
	protected function checkPermit2($c,$a){
		$session = session(C('SESSION_KEY'));
		$permit = C('PERMIT');
		if ($session['level']==2 && isset($permit[$c][$a]) && !in_array($permit[$c][$a],explode(',',$session['permit']))) $this->failed('权限不足！',0,2);
	}
}